{{- define "kube_router_resources" }}
cpu: 20m
memory: 50Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: kube-router
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "kube-router" "workload-resource-policy.deckhouse.io" "every-node")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: DaemonSet
    name: kube-router
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "kube-router"
      minAllowed:
        {{- include "kube_router_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 40m
        memory: 100Mi
{{- end }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-router
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "kube-router")) | nindent 2 }}
spec:
  selector:
    matchLabels:
      app: kube-router
  template:
    metadata:
      labels:
        app: kube-router
    spec:
      {{- include "helm_lib_priority_class" (tuple . "cluster-medium") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_root" . | nindent 6 }}
      imagePullSecrets:
      - name: deckhouse-registry
      serviceAccountName: network-policy-engine
      containers:
      - name: kube-router
        {{- include "helm_lib_module_container_security_context_privileged" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "kubeRouter") }}
        args:
        - --run-router=false
        - --run-firewall=true
        - --run-service-proxy=false
        - --metrics-path=/metrics
        - --metrics-port=10445
        - --health-port=10444
        ports:
        - containerPort: 10445
          name: http-metrics
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10444
          initialDelaySeconds: 10
          periodSeconds: 3
        volumeMounts:
        - name: lib-modules
          mountPath: /lib/modules
          readOnly: true
        - name: xtables-lock
          mountPath: /run/xtables.lock
          readOnly: false
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "kube_router_resources" . | nindent 12 }}
{{- end }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      {{- include "helm_lib_tolerations" (tuple . "any-node") | nindent 6 }}
      volumes:
      - name: lib-modules
        hostPath:
          path: /lib/modules
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate
